package com.yolo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.yolo.util.SmsUtil;
import com.yolo.service.SmsSendService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: SmsSendServiceImpl
 * Description:
 * date: 2021/4/19 15:51
 *
 * @author 须臾
 * @since JDK 1.8
 */
@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Override
    public boolean send(String phoneNum, String templateCode,String code){
        try {
            Client client = SmsUtil.createClient("LTAI5tSB63zngN3jb9PDYvVL", "FeQpJZYEm17OEWyjqnjqcUvQKsVrfO");
            Map<String, Object> map=new HashMap<>();
            map.put("code",code);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phoneNum)//手机号
                    .setSignName("生活中的yoluo")//签名
                    .setTemplateCode("SMS_215070139")//模板
                    .setTemplateParam(JSONObject.toJSONString(map));//验证码
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            System.out.println(sendSmsResponse.getBody());
            if (sendSmsResponse.getBody().getCode().equals("OK")) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return false;
    }
}
